package com.magicode.screen.images;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Environment;
import android.view.View;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 将多张图片拼接在一起
 * @author yw-tony
 *
 */
public class JointBitmapView extends View {
    List<Bitmap> list = new ArrayList<Bitmap>();
    private Bitmap bitmap;
    private String imgPath="";
//    private final static String FILE_SAVEPATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/magicode";
    private final static String FILE_SAVEPATH =Environment.getExternalStorageDirectory()
                                                    + File.separator +"screen" + File.separator +"picture";
    public JointBitmapView(Context context,List<Bitmap> listBit) {
        super(context);
        bitmap = newBitmap(listBit);
//        new Thread(){
//            public void run() {
                //保存拼接好的bitmap
//                saveBitmapToSdcard(bitmap);
//            };
//        }.start();

    }
    /**
     * 拼接图片
     * List<Bitmap> listBit
     * @return 返回拼接后的Bitmap
     */
    private Bitmap newBitmap(List<Bitmap> listBit) {
        Bitmap bitmap =null;
        if (null!=listBit && listBit.size()>0){

            int width = listBit.get(0).getWidth();
        //    int height = bit1.getHeight() + bit2.getHeight();
            int height =0;
            for(Bitmap bit:listBit){
                height+=bit.getHeight();
            }
            //创建一个空的Bitmap(内存区域),宽度等于第一张图片的宽度，高度等于两张图片高度总和
             bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            //将bitmap放置到绘制区域,并将要拼接的图片绘制到指定内存区域
            Canvas canvas = new Canvas(bitmap);
            for(int i=0;i<listBit.size();i++){
                if(i==0){
                    canvas.drawBitmap(listBit.get(i), 0, -250, null);

                }else {
                    canvas.drawBitmap(listBit.get(i), 0, -250+listBit.get(i-1).getHeight(), null);
                }
            }
//            canvas.drawBitmap(bit1, 0, 0, null);
//            canvas.drawBitmap(bit2, 0, bit1.getHeight(), null);
        }
        return bitmap;
    }
//    @Override
//    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
//        canvas.drawBitmap(bitmap, 0, 0, null);
//        bitmap.recycle();
//    }
    /**
     * 将bitmap保存到内存卡中
     * @param bitmap
     */
    private void saveBitmapToSdcard(Bitmap bitmap){
        String storageState = Environment.getExternalStorageState();
        if (storageState.equals(Environment.MEDIA_MOUNTED)) {
            File myDir = new File(FILE_SAVEPATH);
            if (!myDir.exists()) {//如果目录不存在就创建目录
                myDir.mkdirs();
            }
            String fname = "screen.jpg";
            File file = new File(myDir, fname);
            if (file.exists()) {
                file.delete();
            }
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            imgPath=file.getPath();
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(file);


                if (bitmap != null) {
                /*
                 * 三个参数的含义分别是：
                 * 1.保存图片的格式
                 * 2.标识图片质量0~100.质量越小压缩的越小（这里设置100标识不压缩）。另外如果图片是png格式，压缩是无损的，将忽略此参数（设置无效）
                 * 3.向OutputStream写入图片数据
                 */
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                    out.flush();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (out != null) {
                        out.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public String getImgPath() {
        return imgPath;
    }

    public void setImgPath(String imgPath) {
        this.imgPath = imgPath;
    }
}